package com.example.haojiutao.controller.admin;


import com.example.haojiutao.common.Result;
import com.example.haojiutao.model.domain.User;
import com.example.haojiutao.service.UserService;
import com.example.haojiutao.utils.*;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
//@Controller
/*
 * @Description: 用户控制层
 * @Author: mu_junyu
 * @CreateTime: 2023/9/21 8:35 
 * @param: null 
 * @return: null
 **/

public class UserAdminController {

    @Autowired
    private UserService userService;
    //获取个人信息

    /*
     * @Description: //为普通用户提供控制器，用户个人信息查案，注册登录
     * @Author: mu_junyu
     * @CreateTime: 2023/9/20 10:56
     * @return: com.example.musicserver.common.Result
     **/
    @RequestMapping(method={RequestMethod.GET,RequestMethod.POST}, value="/admin/user/info")
    /*
    * @Description: TODO 需要从登录对象中获取当前用户ID
    * @Author: yu_jianlong
    * @CreateTime: 2023/11/9 15:03
     * @return: com.example.haojiutao.common.Result
    **/
    public Result getUserInfo(){

//        int userId=64;
//        User user=userService.selectById(userId);
        //从当前线程中读取登录用户信息
        User user = (User) H5LoginUserUtil.getLoginUser();
        System.out.println("==========="+user);
        return Result.success("查询成功",user);
    }


    /*
     * @Description: TODO用户修改个人信息
     * @Author: mu_junyu
     * @CreateTime: 2023/9/21 9:23
     * @param: user
     * @return: com.example.musicserver.common.Result
     **/
    @PutMapping("/admin/user/info")
    public int updateInfo(@RequestBody  User user){
        log.info("待修改的个人信息 ：{}" ,user);
        return userService.updateUser(user);

    }
    /*
     * @Description: TODO添加用户
     * @Author: mu_junyu
     * @CreateTime: 2023/11/24 19:57
     * @param: user
     * @return: com.example.haojiutao.common.Result
     **/
    @PostMapping("/admin/user/user")
    public Result adduser(@RequestBody User user) {

        log.info("联系商家",user);

        return userService.insertuser(user);
    }

    /*
     * @Description: 删除用户
     * @Author: mu_junyu
     * @CreateTime: 2023/9/25 17:00
     * @param: id
     * @return: com.example.secondhand.common.Result
     **/
    @DeleteMapping("/admin/user/")
    public int delete(Integer id){
        return userService.deleteById(id);
    }
    @DeleteMapping("/admin/user/{id}")
    public Result delete1(@PathVariable("id")  Integer id){

//        return userService.deleteById(id);
        int rows = userService.deleteById(id);
        if(rows == 1){
            return Result.success("删除成功");
        }else{
            return Result.faile("删除失败");
        }
    }

/*
 * @Description: 列表查询
 * @Author: mu_junyu
 * @CreateTime: 2023/9/25 17:01
 * @param: user
 * @return: com.example.secondhand.common.Result
 **/
@CrossOrigin(origins = {"http://localhost:5173"}, allowCredentials = "true")
    @GetMapping("/admin/user")
    public Result list( User user){
        log.info("列表查询参数 ：{}" ,user);
        List<User> users =userService.listByUser(user);
        PageInfo<User> pageInfo=new PageInfo<>(users);
        return Result.success("查询成功",pageInfo);
    }
    /*
     * @Description: 根据用户ID查询用户
     * @Author: mu_junyu
     * @CreateTime: 2023/11/27 17:19
     * @param: id
     * @return: com.example.haojiutao.common.Result
     **/
    @GetMapping("/admin/user/{id}")   // 请求地址：/admin/songlist/1
    public Result detail(@PathVariable("id") Integer id){
        User user = userService.getById(id);
        return Result.success("查询成功", user);
    }
    @CrossOrigin(origins={"http://localhost:5173"}, allowCredentials = "true")
    @GetMapping("/admin/user/avg")
    public Result getRecommendUseravg(){
        User user=new User();
        user.setPageSize(5); //显示6条；
        return Result.success("查询成功！",
                userService.getRecommendUseravg(user));
    }




}
